﻿using System;
using System.Collections;
using System.Web;
using viNews.Classes;
using viNews.Controllers;
using visinia.SmartEngine;

namespace viNews.HttpModules
{
    public class UrlRewriter : BaseUrlRewriter
    {
        public override string RewriteUrl(HttpApplication httpApp, IEnumerator urlEnum)
        {
            HttpContext context = ((HttpApplication)httpApp).Context;
            return HandleURL(context, urlEnum);
        }

        /*****************************************************************************************/
        // Entry URL = News/Category/NewsId/some.thing.imp.aspx
        /*****************************************************************************************/
        /****** OR *******/
        /*****************************************************************************************/
        // Entry URL = News/Category/SubCategory/NewsId/some.thing.imp.aspx
        /*****************************************************************************************/

        private string HandleURL(HttpContext context, IEnumerator enu)
        {
            string root = WebRoots.GetRelativeWebRoot;
            string url = context.Request.Url.AbsolutePath.ToLower();            
            string rel = url.Remove(0, root.Length);
            string[] segments = rel.Split('/');
                        
            if (!enu.MoveNext())// show viNews front page                    
                return root + "Default.aspx?PageId=" + BaseController.FrontPageId;
            /************************************************/
            // handle Category
            /***********************************************/
            Category category = CategoryController.GetCategory(TrimExtension((string)enu.Current), 0);
            if (category == null)
            {
                // SHOW ERROR 
                return null;
            }
            else
            {
                // it is a valid category
                if (!enu.MoveNext())
                {
                    //if can't move further just show this category assigned page
                    if (category.PageId > 0)
                        return root + "Default.aspx?PageId=" + category.PageId + "&CategoryId=" + category.CategoryId;
                    else
                        // SHOW ERROR 
                        return null;
                }
                else
                {
                    // show the news with this category
                    
                    if (segments.Length == 4)
                    {
                        int NewsId = 0;
                        if (Int32.TryParse((string)enu.Current, out NewsId))
                        {
                            News news = NewsController.GetNews(NewsId);
                            if (news != null)
                            {
                                if (category.ContentPageId == 0)
                                    return root + "Default.aspx?PageId=" + BaseController.ContentPageId + "&NewsId=" + news.NewsId + "&CategoryId=" + category.CategoryId;
                                else
                                    return root + "Default.aspx?PageId=" + category.ContentPageId + "&NewsId=" + news.NewsId + "&CategoryId=" + category.CategoryId;
                            }
                        }
                    }
                    else
                    {
                        /************************************************/
                        // handle SubCategory
                        /***********************************************/
                        string MayBeSubCategory = TrimExtension((string)enu.Current);
                        Category SubCategory = CategoryController.GetCategory(MayBeSubCategory, category.CategoryId);
                        if (SubCategory == null)
                        {
                            // SHOW ERROR PAGE, no such subcategory name exist
                            return null;
                        }
                        else
                        {
                            if (!enu.MoveNext()) //show subcategory page  
                            {
                                if (SubCategory.PageId > 0)
                                    return root + "Default.aspx?PageId=" + SubCategory.PageId + "&CategoryId=" + category.CategoryId + "&SubCategoryId=" + SubCategory.CategoryId;
                                else
                                    // SHOW ERROR 
                                    return null;
                            }
                            else
                            {
                                if (segments.Length == 5)// it is sure now that tempName  is sub category name
                                {
                                    int NewsId = 0;
                                    if (Int32.TryParse((string)enu.Current, out NewsId))
                                    {
                                        News news = NewsController.GetNews(NewsId);
                                        if (news != null)
                                        {   // show news in subcategory page                                                
                                            if (category.ContentPageId == 0)
                                                return root + "Default.aspx?PageId=" + BaseController.ContentPageId + "&NewsId=" + news.NewsId + "&CategoryId=" + category.CategoryId + "&SubCategoryId=" + SubCategory.CategoryId;
                                            else
                                                return root + "Default.aspx?PageId=" + category.ContentPageId + "&NewsId=" + news.NewsId + "&CategoryId=" + category.CategoryId + "&SubCategoryId=" + SubCategory.CategoryId;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }

        private string TrimExtension(string url)
        {
            if (url.EndsWith(".aspx"))
                return url.Remove(url.LastIndexOf('.'));
            else
                return url;            
        }
    }
}
